{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Callysto Banner](https://raw.githubusercontent.com/Ariel-VB/Central-Limit-Theorem/master/Callysto_Notebook-Banner_Top_06.06.18.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<script>\n",
       "  function code_toggle() {\n",
       "    if (code_shown){\n",
       "      $('div.input').hide('500');\n",
       "      $('#toggleButton').val('Show Code')\n",
       "    } else {\n",
       "      $('div.input').show('500');\n",
       "      $('#toggleButton').val('Hide Code')\n",
       "    }\n",
       "    code_shown = !code_shown\n",
       "  }\n",
       "  \n",
       "  $( document ).ready(function(){\n",
       "    code_shown=false;\n",
       "    $('div.input').hide()\n",
       "  });\n",
       "</script>\n",
       "<form action=\"javascript:code_toggle()\"><input type=\"submit\" id=\"toggleButton\" value=\"Show Code\"></form>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Hide code\n",
    "from IPython.display import HTML\n",
    "\n",
    "HTML('''<script>\n",
    "  function code_toggle() {\n",
    "    if (code_shown){\n",
    "      $('div.input').hide('500');\n",
    "      $('#toggleButton').val('Show Code')\n",
    "    } else {\n",
    "      $('div.input').show('500');\n",
    "      $('#toggleButton').val('Hide Code')\n",
    "    }\n",
    "    code_shown = !code_shown\n",
    "  }\n",
    "  \n",
    "  $( document ).ready(function(){\n",
    "    code_shown=false;\n",
    "    $('div.input').hide()\n",
    "  });\n",
    "</script>\n",
    "<form action=\"javascript:code_toggle()\"><input type=\"submit\" id=\"toggleButton\" value=\"Show Code\"></form>''')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The 21 Coin Game\n",
    "\n",
    "Hello! Welcome to the 21 coin game. \n",
    "\n",
    "<ins>The Rules:</ins>\n",
    "Imagine that there is a stack of 21 coins sitting on the table in front of you. You and a partner will take turns, alternatively removing coins from the pile. On each turn you can take either 1 or 2 coins from the pile. You can't skip a turn, and you can't remove more than 2 coins. The person who takes the last coin from the table wins the game, and gets to keep all of the money.\n",
    "\n",
    "<ins>Getting Started:</ins>\n",
    "Grab a partner, two different coloured pens (to keep track of how many coins are left after each of your turns), and a piece of paper. We're going to start off by playing against each other, and then we will challenge a computer program using the strategies we have learned! Good luck."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Importing the tools we need:\n",
    "import ipywidgets as widgets\n",
    "from IPython.display import clear_output\n",
    "from ipywidgets import widgets, Layout, Button, Box\n",
    "from IPython.display import clear_output\n",
    "import random\n",
    "\n",
    "#The needed global pieces\n",
    "box_layout = Layout(justify_content = \"center\")\n",
    "turn = \"F\"\n",
    "coins = 21\n",
    "first_or_second = 0\n",
    "\n",
    "#Creating the first or second buttons\n",
    "go_first_button = widgets.Button(\n",
    "    description='First',\n",
    "    disabled=False,\n",
    "    button_style='info', # 'success' (green), 'info' (blue), 'warning' (orange), 'danger' (red) or '' (white)\n",
    "    tooltip='Click me',\n",
    "    icon=''\n",
    ")\n",
    "go_second_button = widgets.Button(\n",
    "    description='Second',\n",
    "    disabled=False,\n",
    "    button_style='info', # 'success' (green), 'info' (blue), 'warning' (orange), 'danger' (red) or '' (white)\n",
    "    tooltip='Click me',\n",
    "    icon=''\n",
    ")\n",
    "\n",
    "#Laying out the buttons\n",
    "button_list_3 = [go_first_button,go_second_button]\n",
    "button_box_3 = Box(children = button_list_3, layout = box_layout)\n",
    "\n",
    "#Defining the functions which are called when the first and second buttons are pressed.\n",
    "def go_first(w):\n",
    "    global turn\n",
    "    turn = 'P'\n",
    "    clear_output()\n",
    "    game(turn)\n",
    "def go_second(w):\n",
    "    global turn\n",
    "    turn = 'C'\n",
    "    clear_output()\n",
    "    game(turn)\n",
    "    \n",
    "#Creating the coin-taking buttons\n",
    "one_coin_button = widgets.Button(\n",
    "    description='Take 1 coin',\n",
    "    disabled=False,\n",
    "    button_style='info', # 'success' (green), 'info' (blue), 'warning' (orange), 'danger' (red) or '' (white)\n",
    "    tooltip='Click me',\n",
    "    icon=''\n",
    ")\n",
    "two_coins_button = widgets.Button(\n",
    "    description='Take 2 coins',\n",
    "    disabled=False,\n",
    "    button_style='info', # 'success' (green), 'info' (blue), 'warning' (orange), 'danger' (red) or '' (white)\n",
    "    tooltip='Click me',\n",
    "    icon=''\n",
    ")\n",
    "\n",
    "#Laying out the buttons\n",
    "button_list = [one_coin_button,two_coins_button]\n",
    "button_box = Box(children = button_list, layout = box_layout)\n",
    "\n",
    "#Defining the functions which are called when the take one and take two buttons are pressed.\n",
    "\n",
    "#This code is run when the player clicks the take one button.\n",
    "def take_one(w):\n",
    "    global coins\n",
    "    coins = coins-1\n",
    "    \n",
    "    #Update the coin total here\n",
    "    print(\"You have taken 1 coin from the pile. The total number of remaining coins is: \" + str(coins))\n",
    "    clear_output()\n",
    "    \n",
    "    #Switch the turn to the computer\n",
    "    turn = \"C\"\n",
    "    \n",
    "    #Runs the game function again (but only after a button has been clicked an a move has been made!)\n",
    "    game(turn)\n",
    "    \n",
    "#This code is run when the player clicks the take two button.\n",
    "def take_two(w):\n",
    "    global coins\n",
    "    coins = coins-2\n",
    "    \n",
    "    #Update the coin total here\n",
    "    print(\"You have taken 2 coins from the pile, leaving \" + str(coins) +\" coins.\")\n",
    "    \n",
    "    #Commented out for the same reason as before\n",
    "    clear_output()\n",
    "    \n",
    "    #Switch the turn to the computer\n",
    "    turn = \"C\"\n",
    "    \n",
    "    #Runs the game function again\n",
    "    game(turn)\n",
    "    \n",
    "#Creating the yes and no buttons\n",
    "yes_button = widgets.Button(\n",
    "    description='Yes',\n",
    "    disabled=False,\n",
    "    button_style='success', # 'success' (green), 'info' (blue), 'warning' (orange), 'danger' (red) or '' (white)\n",
    "    tooltip='Click me',\n",
    "    icon=''\n",
    ")\n",
    "no_button = widgets.Button(\n",
    "    description='No',\n",
    "    disabled=False,\n",
    "    button_style='danger', # 'success' (green), 'info' (blue), 'warning' (orange), 'danger' (red) or '' (white)\n",
    "    tooltip='Click me',\n",
    "    icon=''\n",
    ")\n",
    "\n",
    "#Laying out the buttons\n",
    "box_layout = Layout(justify_content = \"center\")\n",
    "button_list_2 = [yes_button,no_button]\n",
    "button_box_2 = Box(children = button_list_2, layout = box_layout)\n",
    "\n",
    "#Defining the functions that are run when the yes and no buttons are presse\n",
    "\n",
    "def say_yes(w):\n",
    "    global coins\n",
    "    global turn\n",
    "    global first_or_second\n",
    "    coins = 21\n",
    "    turn = 'F'\n",
    "    first_or_second = ' '\n",
    "    clear_output()\n",
    "    game(turn)\n",
    "def say_no(w):\n",
    "    clear_output()\n",
    "    print(\"Thanks for playing! See you next time.\")\n",
    "    \n",
    "#Defining the replay function\n",
    "def replay():\n",
    "        print(\"Thank you for playing! Would you like to play again?\")\n",
    "        display(button_box_2) \n",
    "        \n",
    "\n",
    "#Main game function, takes turn as input\n",
    "def game(turn):\n",
    "    global coins\n",
    "    global first_or_second\n",
    "    \n",
    "    if coins == 0:\n",
    "        \n",
    "        if turn == 'C':\n",
    "            print(\"Congratulations! You win!\")\n",
    "        elif turn == 'P':\n",
    "            print(\"The computer has won.\")\n",
    "        \n",
    "        replay()\n",
    "    \n",
    "    #If turn is 1, only this section runs\n",
    "    elif turn == \"F\":\n",
    "        \n",
    "        #Do all the stuff you need to do before the game starts here\n",
    "        print(\"Hello! Welcome to the 21 coin game!\")\n",
    "        \n",
    "        #Sets the turn for the next player (you'll want to make sure this is whatever player is chosen to go first)\n",
    "        \n",
    "        print(\"Would you like to play first or second?\")\n",
    "        display(button_box_3)\n",
    "        \n",
    "    #Runs if it is the player's turn    \n",
    "    elif turn == \"P\":\n",
    "        \n",
    "        print(\"How many coins would you like to take?\")\n",
    "        \n",
    "        #The only thing that happens in this section is displaying the buttons (and printing stuff if you want to)\n",
    "        #Don't forget to only display one button if there is 1 coin left\n",
    "        if coins > 1:\n",
    "            display(button_box)\n",
    "        elif coins == 1:\n",
    "            display(one_coin_button)\n",
    "        else:\n",
    "            pass\n",
    "        \n",
    "    #Runs if it is the computer's turn\n",
    "    elif turn == \"C\":\n",
    "        \n",
    "        #Here you will want to have the computer decide what they will do, and update the coins\n",
    "        option1 = coins - 1\n",
    "        option2 = coins - 2\n",
    "        \n",
    "        print(\"There are \" + str(coins) + \" coins in the pile.\")\n",
    "        \n",
    "        if option1%3 == 0:\n",
    "            coins = coins - 1\n",
    "            print(\"The computer has taken 1 coin from the pile, leaving \" + str(coins) + \" coins.\")\n",
    "        \n",
    "        elif option2%3 == 0:\n",
    "            coins = coins - 2\n",
    "            print(\"The computer has taken 2 coins from the pile, leaving \" + str(coins) + \" coins.\")\n",
    "        \n",
    "        else:\n",
    "            computer_taken = random.randint(1,2)\n",
    "            \n",
    "            if computer_taken == 1:\n",
    "                coins = coins - 1\n",
    "                print(\"The computer has taken 1 coin from the pile, leaving \" + str(coins) + \" coins.\")\n",
    "            else:\n",
    "                coins = coins - 2\n",
    "                print(\"The computer has taken 2 coins from the pile, leaving \" + str(coins) + \" coins.\")\n",
    "    \n",
    "        #Turn is switched to the player\n",
    "        turn = \"P\"\n",
    "        \n",
    "        #Calls the game function again\n",
    "        game(turn)\n",
    "\n",
    "#Button clicking redirect\n",
    "\n",
    "#First and second buttons\n",
    "go_first_button.on_click(go_first)\n",
    "go_second_button.on_click(go_second)\n",
    "\n",
    "#One coin and two coin buttons\n",
    "one_coin_button.on_click(take_one)\n",
    "two_coins_button.on_click(take_two)\n",
    "\n",
    "#Yes and no buttons\n",
    "yes_button.on_click(say_yes)\n",
    "no_button.on_click(say_no)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Let's Play!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello! Welcome to the 21 coin game!\n",
      "Would you like to play first or second?\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "165111ce1f7648f499268a962b586ec2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Box(children=(Button(button_style='info', description='First', style=ButtonStyle(), tooltip='Click me'), Butto…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "game(turn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generalizing the Coin Game\n",
    "\n",
    "In the original game, there were 21 coins at the start.  Let's change 21 to ANY number of coins at the start.  You choose.\n",
    "\n",
    "In the original game, you could take up to 2 coins on any turn.  Let's change 2 to ANY number of coins you can take.  You choose.\n",
    "\n",
    "Once again, the person who takes the last coin from the table wins the game.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "box_layout = Layout(justify_content = \"center\")\n",
    "\n",
    "turn = \"F\"\n",
    "\n",
    "coins = 0\n",
    "taken_coins = 0\n",
    "player_turn = 0\n",
    "game_over = 0\n",
    "\n",
    "def replay():\n",
    "    global turn\n",
    "    global coins\n",
    "    global first_or_second\n",
    "    global taken_coins\n",
    "    global player_turn\n",
    "    global game_over\n",
    "    play_again = None\n",
    "    \n",
    "    while play_again != 'Yes' and play_again != 'yes' and play_again != 'No' and play_again != 'no':\n",
    "        play_again = input(\"Thank you for playing! Would you like to play again?\")\n",
    "            \n",
    "    clear_output()\n",
    "    \n",
    "    if play_again == 'Yes' or play_again == 'yes':\n",
    "        turn = \"F\"\n",
    "        coins = 0\n",
    "        taken_coins = 0\n",
    "        player_turn = 0\n",
    "            \n",
    "        game(turn)\n",
    "            \n",
    "    elif play_again == 'No' or play_again == 'no':\n",
    "        turn = 'Q'\n",
    "        print(\"Thank you for playing! See you next time.\")\n",
    "    else: \n",
    "        print(\"An unknown error has occured. Please try again later.\")\n",
    "\n",
    "\n",
    "#Main game function, takes turn as input\n",
    "def game(turn):\n",
    "    global coins\n",
    "    global first_or_second\n",
    "    global taken_coins\n",
    "    global player_turn\n",
    "    global game_over\n",
    "    \n",
    "    #Only runs on the first turn\n",
    "    if turn == \"F\":\n",
    "        \n",
    "        #Do all the stuff you need to do before the game starts here\n",
    "        \n",
    "        while coins == 0:\n",
    "            coins = int(input('How many coins would you like to play with? '))\n",
    "            \n",
    "        while taken_coins == 0:\n",
    "            taken_coins = int(input('What is the maximal number of coins you would like a player to be able to take on their turn? '))\n",
    "\n",
    "        while player_turn != 1 and player_turn != 2:\n",
    "            player_turn = int(input(\"Would you like to play first or second? Please enter 1 for first and 2 for second. \"))\n",
    "        \n",
    "        clear_output()\n",
    "        \n",
    "        if player_turn == 1:\n",
    "            turn = 'P'\n",
    "        elif player_turn == 2:\n",
    "            turn = 'C'\n",
    "        else:\n",
    "            turn = 'F'\n",
    "    \n",
    "        game(turn)\n",
    "    \n",
    "    #win check\n",
    "    elif coins == 0 and game_over != 1:\n",
    "        \n",
    "        if turn == 'C':\n",
    "            print(\"Congratulations! You win!\")\n",
    "        elif turn == 'P':\n",
    "            print(\"The computer has won.\")\n",
    "        \n",
    "        replay()\n",
    "        \n",
    "    #Runs if it is the player's turn    \n",
    "    elif turn == \"P\":\n",
    "        \n",
    "        taking_coins = coins + 100\n",
    "        \n",
    "        while taking_coins > coins or taking_coins > taken_coins:\n",
    "            taking_coins = int(input(\"How many coins would you like to take?\"))\n",
    "        \n",
    "        clear_output()\n",
    "        \n",
    "        coins = coins - taking_coins\n",
    "        \n",
    "        turn = 'C'\n",
    "        game(turn)\n",
    "        \n",
    "    #Runs if it is the computer's turn\n",
    "    elif turn == \"C\":\n",
    "        \n",
    "        print(\"There are \" + str(coins) + \" coins in the pile.\")\n",
    "        \n",
    "        #Here you will want to have the computer decide what they will do, and update the coins\n",
    "        for x in range(1,taken_coins+1):\n",
    "            if (coins - x)%(taken_coins+1) == 0:\n",
    "                coins = coins - x\n",
    "                print(\"The computer has taken \" + str(x) + ' coins from the pile, leaving ' + str(coins) + ' coins.')\n",
    "                turn = 'P'\n",
    "                game(turn)\n",
    "            else:\n",
    "                continue\n",
    "                \n",
    "        computer_taken = random.randint(1,taken_coins)\n",
    "        \n",
    "        coins = coins - computer_taken\n",
    "        print(\"The computer has taken \" + str(computer_taken) + ' coins from the pile, leaving ' + str(coins) + ' coins.')\n",
    "        \n",
    "        #Turn is switched to the player\n",
    "        turn = \"P\"\n",
    "        \n",
    "        #Calls the game function again\n",
    "        game(turn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Let's Play!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "game(turn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Callysto Banner](https://github.com/Ariel-VB/Central-Limit-Theorem/blob/master/Callysto_Notebook-Banners_Bottom_06.06.18.jpg?raw=true)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
